package bbs.controller;


import bbs.annotation.GlobalInterceptor;
import bbs.annotation.VerifyParam;
import bbs.controller.base.ABaseController;
import bbs.entity.constants.Constants;
import bbs.entity.dto.FileUploadDto;
import bbs.entity.enums.FileUploadTypeEnum;
import bbs.entity.po.ForumBoard;
import bbs.entity.vo.ResponseVO;
import bbs.service.ForumBoardService;
import bbs.utils.FileUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController("forumBoardController")
@RequestMapping({"/board"})
public class ForumBoardController extends ABaseController {
    @Resource
    private ForumBoardService forumBoardService;

    @Resource
    private FileUtil fileUtil;

    @RequestMapping({"/loadBoard"})
    public ResponseVO loadBoard() {
        return getSuccessResponseVO(this.forumBoardService.getBoardTree(null));
    }

    @RequestMapping({"/saveBoard"})
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO saveBoard(Integer boardId, @VerifyParam(required = true) Integer pBoardId,@VerifyParam(required = true) String boardName, String boardDesc, Integer postType, MultipartFile cover)
    {
        ForumBoard forumBoard = new ForumBoard();
        forumBoard.setBoardDesc(boardDesc);
        forumBoard.setPostType(postType);
        forumBoard.setBoardName(boardName);
        forumBoard.setBoardId(boardId);
        forumBoard.setpBoardId(pBoardId);

        if (cover != null) {
            FileUploadDto uploadDto = fileUtil.uploadFile2Local(cover, Constants.FILE_FOLDER_IMAGE, FileUploadTypeEnum.ARTICLE_COVER);
            forumBoard.setCover(uploadDto.getLocalPath());
        }
        this.forumBoardService.saveForumBoard(forumBoard);
        return getSuccessResponseVO(this.forumBoardService.getBoardTree(null));
    }

    @RequestMapping({"/delBoard"})
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO delBoard(@VerifyParam(required = true) Integer boardId)
    {
        this.forumBoardService.deleteForumBoardByBoardId(boardId);
        return getSuccessResponseVO(this.forumBoardService.getBoardTree(null));
    }

    @RequestMapping({"/changeBoardSort"})
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO changeBoardSort(@VerifyParam(required = true) String boardIds)
    {
        forumBoardService.changeSort(boardIds);
        return getSuccessResponseVO(null);
    }
}
